package com.example.sunsg.okhttptest.adapter;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;

import com.example.sunsg.okhttptest.Manager.FrescoManager;
import com.example.sunsg.okhttptest.bean.LocalImageRequestBuilder;
import com.example.sunsg.okhttptest.bean.PreviewImage;
import com.example.sunsg.okhttptest.customview.PhotoDraweeView;
import com.example.sunsg.okhttptest.util.DisplayUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;

/**
 * Created by sunsg on 16/3/7.
 */
public class ImagePreviewAdapter extends BasePagerAdapter<PreviewImage>{
    private static final float scaleRate = 0.7f;

    private float screenRate;
    private float screenWidth;
    private float screenHeight;

    public ImagePreviewAdapter(Context context) {
        super(context);
        screenWidth = DisplayUtils.getScreenWidth(context);
        screenHeight = DisplayUtils.getScreenHeight(context);
        screenRate = screenWidth * 1.0f / screenHeight;
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public PreviewImage getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new PhotoDraweeView(mContext);
        }

        final PhotoDraweeView photoDraweeView = (PhotoDraweeView) convertView;

        PreviewImage image = getItem(position);
        int width;
        int height;
        if (image.getWidth() > 0 && image.getHeight() > 0) {
            float rate = image.getWidth() * 1.0f / image.getHeight();
            if (rate > screenRate) {
                width = (int) Math.min(screenWidth * scaleRate, image.getWidth());
                height = (int) (width / rate);
            } else {
                height = (int) Math.min(screenHeight * scaleRate, image.getHeight());
                width = (int) (height * rate);
            }
            Uri uri = null;
            if(image.isLocalFilePath()){
                uri = Uri.parse(FrescoManager.getFilePath(image.getPath()));
            }else{
                uri = Uri.parse(image.getPath());
            }

            ImageRequest request = LocalImageRequestBuilder
                    .newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(width, height)).setAutoRotateEnabled(true).build();
            PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
            controller.setImageRequest(request);
            controller.setOldController(photoDraweeView.getController());
            controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (imageInfo == null) {
                        return;
                    }
                    photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                }
            });
            photoDraweeView.setController(controller.build());
        }
        convertView = photoDraweeView;
        convertView.setId(position);
        return convertView;
    }


}
